//
//  SCB_Today_ViewController.m
//  Time
//
//  Created by dllo on 16/7/13.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Today_ViewController.h"
#import "SCB_Today_View.h"
#import "SCB_Today_TodayContentModel.h"
#import "SCB_Today_Debris_ContentModel.h"
#import "SCB_Radio_ViewController.h"
#import "SCB_Today_Default_detail_ViewController.h"
#import "SCB_Today_Music_ViewController.h"
#import "SCB_Today_List_ViewController.h"

@interface SCB_Today_ViewController ()<SCB_Today_ViewDelegate>

@property (nonatomic,retain) SCB_Today_View *todayContentView;

@property (nonatomic,assign) NSInteger todayPageNum;

@property (nonatomic,assign) NSInteger debrisPageSize;

@property (nonatomic,assign) BOOL isDown;

@property (nonatomic,retain) UIView *todayTitleView;

@property (nonatomic,retain) UIButton *mainButton;

@property (nonatomic,retain) UIButton *debrisButton;

@property (nonatomic,retain) UIButton *todayButton;

@property (nonatomic,retain) UIView *tagView;

@property (nonatomic,retain) UIButton *tagButton;


@end

@implementation SCB_Today_ViewController
-(void)dealloc{
    [_todayContentView release];
    [_modelArray release];
    [_debrisModelArray release];
    [_todayTitleView release];
    [_mainButton release];
    [_debrisButton release];
    [_todayButton release];
    [_tagView release];
    self.todayContentView.delegate = nil;
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArray = [NSMutableArray arrayWithCapacity:5];
    self.debrisModelArray = [NSMutableArray arrayWithCapacity:5];
    
    self.navigationController.navigationBar.hidden = YES;
    
    [self createMainButton];
    [self createTodayTitleView];
    [self handleData];
    [self debrisHandleData];
    
    self.todayContentView = [[SCB_Today_View alloc] initWithFrame:CGRectMake(0, 88, SWIDTH, self.view.bounds.size.height - 88)];
    
    /**
     *  block传值
     */
    void(^offsetBlock)(BOOL) = ^(BOOL isToday){
        if (isToday) {
            [self debrisButton:self.debrisButton];
        } else {
            [self todayButton:self.todayButton];
        }
    };
    
    self.todayContentView.offsetBlock = offsetBlock;
    
    
    self.todayContentView.delegate = self;
    [self.view addSubview:self.todayContentView];
    [_todayContentView release];
    [self.view bringSubviewToFront:self.mainButton];
    [self.view bringSubviewToFront:self.tagButton];
    
}



-(void)createMainButton{
    self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.mainButton];
    self.mainButton.backgroundColor = [UIColor blackColor];
    [self.mainButton setImage:[UIImage imageNamed:@"列表"] forState:UIControlStateNormal];
    
    
    [self.mainButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(30);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.height.mas_equalTo(30);
    }];
    [self.mainButton addTarget:self action:@selector(mainButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tagButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.tagButton];
    self.tagButton.backgroundColor = [UIColor blackColor];
    [self.tagButton setImage:[UIImage imageNamed:@"标签"] forState:UIControlStateNormal];
    self.tagButton.hidden = YES;
    [self.tagButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.width.mas_equalTo(30);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.height.mas_equalTo(30);
    }];
    
    [self.tagButton addTarget:self action:@selector(tagButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)mainButtonAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)tagButtonAction:(UIButton *)button{
    NSLog(@"标签");
    SCB_Today_List_ViewController *Today_Lisy_VC = [[SCB_Today_List_ViewController alloc] init];
    [self.navigationController pushViewController:Today_Lisy_VC animated:YES];
}

/**
 *  创建标题头
 */
-(void)createTodayTitleView{
    self.todayTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 88)];
    [self.view addSubview:self.todayTitleView];
    self.debrisButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.todayButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.todayTitleView addSubview:self.debrisButton];
    [self.todayTitleView addSubview:self.todayButton];
    

    
    [self.debrisButton setTitle:@"碎片" forState:UIControlStateNormal];
    self.debrisButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [self.debrisButton setTintColor:[UIColor grayColor]];
    
    [self.debrisButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.top.equalTo(self.view.mas_top).offset(50);
        make.width.mas_equalTo(SWIDTH * 0.5);
        make.height.mas_equalTo(38);
    }];
    
    [self.todayButton setTitle:@"今日" forState:UIControlStateNormal];
    self.todayButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
    [self.todayButton setTintColor:[UIColor colorWithRed:154 / 255.0 green:204 / 255.0 blue:51 / 255.0 alpha:1.0]];
    
    [self.todayButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(self.view.mas_top).offset(50);
        make.width.mas_equalTo(SWIDTH * 0.5);
        make.height.mas_equalTo(38);
    }];
    
    self.tagView = [[UIView alloc] initWithFrame:CGRectMake(SWIDTH * 0.75 - 24, 85, 48, 3)];
    self.tagView.backgroundColor = [UIColor colorWithRed:154 / 255.0 green:204 / 255.0 blue:51 / 255.0 alpha:1.0];
    [self.todayTitleView addSubview:self.tagView];
    
    
    [self.debrisButton addTarget:self action:@selector(debrisButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.todayButton addTarget:self action:@selector(todayButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

-(void)debrisButton:(UIButton *)button{
    [self.todayContentView.contentCollectionView setContentOffset:CGPointMake(SWIDTH * 0, 0) animated:YES];
    for (UIButton *btn in self.todayTitleView.subviews){
        [btn setTintColor:[UIColor grayColor]];
    }
    [button setTintColor:[UIColor colorWithRed:154 / 255.0 green:204 / 255.0 blue:51 / 255.0 alpha:1.0]];
    [UIView animateWithDuration:0.5 animations:^{
        [self.tagView setFrame:CGRectMake(SWIDTH * 0.25 - 24, 85, 48, 3)];
    }];
    self.tagButton.hidden = NO;
    NSLog(@"碎片");
}

-(void)todayButton:(UIButton *)button{
    [self.todayContentView.contentCollectionView setContentOffset:CGPointMake(SWIDTH * 1, 0) animated:YES];
    for (UIButton *btn in self.todayTitleView.subviews){
        [btn setTintColor:[UIColor grayColor]];
    }
    [button setTintColor:[UIColor colorWithRed:154 / 255.0 green:204 / 255.0 blue:51 / 255.0 alpha:1.0]];
    [UIView animateWithDuration:0.5 animations:^{
        [self.tagView setFrame:CGRectMake(SWIDTH * 0.75 - 24, 85, 48, 3)];
    }];
    self.tagButton.hidden = YES;
    NSLog(@"今日");
}

-(void)handleData{
    
    if (self.isDown) {
        self.todayPageNum = 1;
    } else {
        self.todayPageNum++;
    }
    
    NSString *urlStr = @"http://api.pianke.me/version4.0/pub/today.php?sig=79935AAB1C83627DE6932AE32C8DA186";

    NSString *body = [NSString stringWithFormat:@"pageNum=%ld&pageSize=10",self.todayPageNum];
    NSDictionary *header = @{@"Authorization":HEADER};
    // 轮播图以外的数据请求
    [SCBNetTool POST:urlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
//        NSLog(@"抓取成功%@",result);
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        if (self.isDown) {
            [self.modelArray removeAllObjects];
            for (NSDictionary *dic in array){
                SCB_Today_TodayContentModel *model = [[SCB_Today_TodayContentModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArray addObject:model];
                [model release];
            }
        } else {
            for (NSDictionary *dic in array){
                SCB_Today_TodayContentModel *model = [[SCB_Today_TodayContentModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.modelArray addObject:model];
                [model release];
            }
        }
        
        self.todayContentView.modelArray = self.modelArray;
    } withFaolure:^(NSError *error) {
        NSLog(@"抓取失败%@",error);
    }];
}

-(void)debrisHandleData{
    if (self.isDown){
        self.debrisPageSize = 10;
    } else {
        self.debrisPageSize = self.debrisPageSize + 10;
    }
    
    
    
    NSString *url = @"http://api.pianke.me/version4.0/timeline/list.php?sig=48424EF679968D74F3C549697DA90768";
//    NSString *body = @"pageNum=1&pageSize=10";
    NSString *body = [NSString stringWithFormat:@"pageNum=1&pageSize=%ld",self.debrisPageSize];
    NSDictionary *header = @{@"Authorization":HEADER};
    [SCBNetTool POST:url withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
//        NSLog(@"抓取成功,%@",result);
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        if (self.isDown){
            [self.debrisModelArray removeAllObjects];
            for (NSDictionary *dic in array){
                SCB_Today_Debris_ContentModel *model = [[SCB_Today_Debris_ContentModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.debrisModelArray addObject:model];
                [model release];
            }
        } else {
            [self.debrisModelArray removeAllObjects];
            for (NSDictionary *dic in array){
                SCB_Today_Debris_ContentModel *model = [[SCB_Today_Debris_ContentModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.debrisModelArray addObject:model];
                [model release];
            }
        }
        self.todayContentView.debrisModelArray = self.debrisModelArray;
        [self.todayContentView.contentCollectionView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"抓取失败,%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadNewData:(BOOL)isDown{
    self.isDown = isDown;
    [self handleData];
}

-(void)loadNewDataForDebris:(BOOL)isDown{
    self.isDown = isDown;
    [self debrisHandleData];
}

-(void)pushToDetail:(NSString *)contentId{
    SCB_Today_Default_detail_ViewController *default_detail_VC = [[SCB_Today_Default_detail_ViewController alloc] init];
    default_detail_VC.contentId = contentId;
    [self.navigationController pushViewController:default_detail_VC animated:YES];
}

-(void)pushToMusic:(NSString *)contentId{
    SCB_Today_Music_ViewController *music_VC = [[SCB_Today_Music_ViewController alloc] init];
    music_VC.contentId = contentId;
    [self.navigationController pushViewController:music_VC animated:YES];
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}


@end
